package cn.vconinfo.base.utils;

import cn.vconinfo.base.enums.MathCompareCalMarkEnum;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * 
 * @author oms
 * @version $Id: NumberUtils.java, v 0.1 2016-7-1 上午11:06:03 oms Exp $
 */
public class NumberUtil {

    /**
     * 判断是否是金额
     * 
     * @param money
     * @return
     */
    public static boolean isMoney(String money) {
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后一位的数字的正则表达式
        Matcher match = pattern.matcher(money);
        if (match.matches() == false) {
            return false;
        } else {
            return true;
        }
    }
    
    /**
     *  当要比较两个字符串是数字时，按照数字比大小，否则按照字符串比较
     * 
     * @param source
     * @param dest
     * @return
     */
    public static int compareTo(String source, String dest) {
        int compare = 0 ;
        if(NumberUtils.isCreatable(cn.vconinfo.base.utils.StringUtils.null2Str(source).trim()) && NumberUtils.isCreatable(cn.vconinfo.base.utils.StringUtils.null2Str(dest).trim())) {
            compare = cn.vconinfo.base.utils.StringUtils.string2BigDecimal(source.trim()).compareTo(cn.vconinfo.base.utils.StringUtils.string2BigDecimal(dest.trim()));
        }else {
            compare = cn.vconinfo.base.utils.StringUtils.null2Str(source).compareTo(cn.vconinfo.base.utils.StringUtils.null2Str(dest));
        }
        
        return compare;
    }
    
    public static void main(String[] args) {
        System.out.println(compareTo("7.0","7.0"));
    }

    /**
     *  根据数据库的比较符号，来判断计算
     * 
     * @param source
     * @param dest
     * @param compareMark
     * @return
     */
    public static boolean compareCalcuMark(String source, String dest, String compareMark) {
        boolean isCompared = false;
        if(StringUtils.isBlank(compareMark) || MathCompareCalMarkEnum.LESS.getCode().equals(compareMark.trim())) {
            isCompared = compareTo(source, dest) < 0;
        }else if(MathCompareCalMarkEnum.LESSEQUAL.getCode().equals(compareMark.trim())) {
            isCompared = compareTo(source, dest) <= 0;
        }else if(MathCompareCalMarkEnum.GREATER.getCode().equals(compareMark.trim())) {
            isCompared = compareTo(source, dest) > 0;
        }else if(MathCompareCalMarkEnum.GREATEREQUAL.getCode().equals(compareMark.trim())) {
            isCompared = compareTo(source, dest) >= 0;
        }else if(MathCompareCalMarkEnum.EQUAL.getCode().equals(compareMark.trim())) {
            isCompared = compareTo(source, dest) == 0;
        }
        
        return isCompared;
    }

    /**
     * 格式化数字
     * 
     * @param number   数字
     * @param pattern  格式
     * @param rm       四舍五入的模式
     * @return
     */
    public static String format(Object number,String pattern,RoundingMode rm){
        if(number==null){
            return "";
        }
        DecimalFormat f =  new DecimalFormat(pattern);
        f.setRoundingMode(rm);
        return f.format(number);
    }
    
    /**
     * 格式化数字
     * 
     * <p>在四舍五入时采用 {@link RoundingMode.HALF_EVEN}模式，也就是'奇进偶不进'的规则</p>
     * 
     * @param number    数字
     * @param pattern   格式
     * @return
     */
    public static String format(Object number,String pattern){
        return format(number, pattern, RoundingMode.HALF_EVEN);
    }
    
    /**
     *  设定数据的精度
     * 
     * @param number
     * @param precision
     * @param rm
     * @return
     */
   public static String format(Object number, Integer precision, RoundingMode rm) {
       if(number==null){
           return "";
       }
       BigDecimal decimal = cn.vconinfo.base.utils.StringUtils.string2BigDecimal(cn.vconinfo.base.utils.StringUtils.null2Str(number)).setScale(precision, rm);
       return decimal.toPlainString();
   }
   
   /**
    *  设定数据的精度
    * 
     * <p>在四舍五入时采用 {@link RoundingMode.HALF_UP}模式，也就是'奇进偶不进'的规则</p>
     * 
    * @param number
    * @param precision
    * @return
    */
   public static String format(Object number, Integer precision) {
       return format(number, precision, RoundingMode.HALF_UP);
   }

   /**
    *  两个数字相加
    * 
    * @param source
    * @param dest
    * @return
    */
   public static BigDecimal add(BigDecimal source, BigDecimal dest) {
       if(source == null ) {
           source = BigDecimal.ZERO;
       }
       if(dest == null) {
           dest = BigDecimal.ZERO;
       }
       
       return source.add(dest);
   }
   
   /**
    *  计算两个字符串数字的差
    * 
    * @param source
    * @param dest
    * @return
    */
   public static BigDecimal subtract(String source, String dest) {
       BigDecimal sourceDec = null;
       if(NumberUtils.isCreatable(source)) {
           sourceDec = new BigDecimal(source);
       }else {
           sourceDec = BigDecimal.ZERO;
       }
       BigDecimal destDec = null;
       if(NumberUtils.isCreatable(dest)) {
           destDec = new BigDecimal(dest);
       }else {
           destDec = BigDecimal.ZERO;
       }
       
       return sourceDec.subtract(destDec);
   }

   public static boolean isDigit(String content){
       String pattern = "^[1-9]\\d*$";
       return Pattern.matches(pattern, content);
   }

}
